package cn.wizzer.framework.filter;

import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.util.TLManager;
import cn.wizzer.framework.util.ValidateUtil;
import io.swagger.annotations.ApiParam;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.JsonFormat;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.view.UTF8JsonView;

import java.lang.reflect.Parameter;
@IocBean(singleton=false)
public class ParamFilter implements ActionFilter {
    private static final Log log = Logs.get();

    @Override
    public View match(ActionContext actionContext) {
        for (Parameter parameter : actionContext.getMethod().getParameters()) {
            ApiParam annotation = parameter.getAnnotation(ApiParam.class);
            if (annotation != null) {
                if (annotation.required()) {
                    if (ValidateUtil.isEmpty(actionContext.getRequest().getParameter(parameter.getName()))) {
                        log.info(parameter.getName() + "为空");
                        return new UTF8JsonView(JsonFormat.compact()).setData(Result.error(-4, parameter.getName() + "不能为空"));
                    }
                }
            }
        }
        //正常处理下一调用链环节
        return null;
    }
}
